<?php
namespace collecter\controllers;

use common\models\User;
use Yii;
use yii\web\Controller;

class LoginController extends Controller{

//    public $enableCsrfValidation = false;

    public function actionLogin(){
        return $this->renderPartial('login');
    }

    public function actionAsyncLogin(){
        $username = isset($_POST["username"]) ? addslashes($_POST["username"]) : "";
        $password = isset($_POST["password"]) ? addslashes($_POST["password"]) : "";

        $rememberMe = true;

//        echo $username."<br>";
//        echo $password."<br>";

        if($username == "admin" && $password == "sdfsdf"){
//            $user = new User();
//            $user->id = 1;
//            $user->username = "admin";

            $user = User::findByUsername($username);
            //var_dump($user);exit;

            Yii::$app->user->login($user, $rememberMe ? 3600*24*30 : 0);
            exit(json_encode(["code"=>1, "info"=>"login success!"]));
        }else{
            exit(json_encode(["code"=>-1, "info"=>"login failed!"]));
        }

    }

    /**
     * 登出
     */
    public function actionLogout(){
        \Yii::$app->user->logout();
        $this->redirect('/login/login.html');
    }

    public function test(){
        echo "test:";
    }

}